package com.xci.core.configuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SwaggerBeanFactoryPostProcessor implements BeanFactoryPostProcessor, EnvironmentAware {

    // private Map<String, Map<String, Object>> configMap = new HashMap<String, Map<String, Object>>();
    //
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        //     DefaultListableBeanFactory defaultListableBeanFactory
        //             = (DefaultListableBeanFactory) beanFactory;

//        //注册Bean定义，容器根据定义返回bean
//        log.info("register personManager1>>>>>>>>>>>>>>>>");
//        BeanDefinitionBuilder beanDefinitionBuilder =
//                BeanDefinitionBuilder.genericBeanDefinition(PersonManager.class);
//        beanDefinitionBuilder.addPropertyReference("personDao", "personDao");
//        BeanDefinition personManagerBeanDefinition = beanDefinitionBuilder.getRawBeanDefinition();
//        defaultListableBeanFactory.registerBeanDefinition("personManager1", personManagerBeanDefinition);

        //注册bean实例
//        log.info("register 实例>>>>>>>>>>>>>>>>");

//        Docket d1 = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .groupName("V1.0")
//                .select()
//                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
//                .paths(PathSelectors.any())
//                .build()
//                .globalOperationParameters(Lists.newArrayList());
//
//        beanFactory.registerSingleton("sysRestApi", d1);
    }

    @Override
    public void setEnvironment(Environment environment) {
//        //获取到前缀是"custom.datasource." 的属性列表值.
//        RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment,"custom.datasource.");
//        //获取到所有数据源的名称.
//        String dsPrefixs = propertyResolver.getProperty("names");
//        String[] dsPrefixsArr = dsPrefixs.split(",");
//        for(String dsPrefix:dsPrefixsArr){
//            Map<String, Object> dsMap = propertyResolver.getSubProperties(dsPrefix + ".");
//            //存放到一个map集合中，之后在注入进行使用.
//            configMap.put(dsPrefix, dsMap);
//        }
    }
}
